package com.xbdun.web.bmp.controller.system;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.constant.XbdunLoginConst;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.enumx.SysOrgEnum.SysOrgAuditingStatusEnum;
import com.xbdun.facade.bmp.request.system.SysOrgAuditingRequest;
import com.xbdun.facade.bmp.request.system.SysOrgPagerRequest;
import com.xbdun.facade.bmp.result.system.SysOrgPagerResult;
import com.xbdun.facade.bmp.service.system.SysOrgFacade;
import com.xbdun.facade.shopbmp.constant.DubboShopBmpConst;
import com.xbdun.facade.shopbmp.service.system.DiictionariesFacade;
import com.xbdun.web.bmp.service.EmailNotifyService;

import lombok.extern.slf4j.Slf4j;

/**
 * 企业信息
 * 检索 	审核
 * @author  伍文强
 *
 */
@Controller
@Slf4j
@XbdunLogin(XbdunLoginConst.XBDADMINT)
public class S100100102Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgFacade sysOrgFacade;
	@Autowired
	EmailNotifyService emailNotifyService;
	//初始化机构字典
	@Reference(registry=DubboShopBmpConst.REGISTRYS)
	DiictionariesFacade diictionariesFacade;
	//企业信息
	@RequestMapping("100100102")
    public String index(){
        return ROOT_PATH+"100100102";
    }
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@RequestMapping("100100102100")
	@ResponseBody
	public SysOrgPagerResult getList(SysOrgPagerRequest request){
		SysOrgPagerResult result = new SysOrgPagerResult();
		try{
			log.info("企业分页查询,请求参数:{}", request);
			if (request.getAuditingStatus()==0&&request.getEnable()==0&&request.getSelectKey()==0) {
				request.setAuditingStatus(SysOrgAuditingStatusEnum.DEFAULT.getKey());
			}
			result = sysOrgFacade.queryPager(request,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("企业分页查询信息失败,入参:{},异常:{}", request, e);
		}
		return result;
	}
	
	/**
	 * 审核
	 * @param request
	 * @return
	 */
	@RequestMapping("100100102101")
	@ResponseBody
	public XbdUnStringResult updateStatus(SysOrgAuditingRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("企业审核,请求参数:{}", request);
			if (request.getId()<=0){
				throw BizException.PARAM_CHKE_FAIL.newInstance("机构ID不能小于等于零");
			}
			result = sysOrgFacade.auditingSysOrg(request, UserInfoUtil.getClient(getRequest()));
			if(SysOrgAuditingStatusEnum.PASS.getKey()==request.getAuditingStatus()){
				//  审核通过初始化机构字典
				try {
					diictionariesFacade.initOrgDictionaries(request.getId());
				} catch (Exception e) {
					log.error("初始化机构字典失败：{}",e);
					e.printStackTrace();
				}
			}
			if(result.getSuccess()){
				SysOrgAuditingStatusEnum enum1 = SysOrgAuditingStatusEnum.getEnum(request.getAuditingStatus());
				//审核通过后需要发送邮件给企业
				if(StringUtils.isNotBlank(request.getOrgEmail())&&SysOrgAuditingStatusEnum.PASS.equals(enum1)){
					emailNotifyService.orgAuditThrough(request.getOrgName(),request.getOrgEmail());
				}
			}
		}catch(Exception e){
			result.setBizException(e);
			log.error("企业审核失败,入参:{},异常:{}", request, e);;
		}
		return result;
	}
}
